<?php
namespace Home\Controller;
use Common\Controller\CommonController;

/**
 * 品牌工类
 * @author 张亚伟 <zhangyw@zhijin101.com>
 */
class BrandController extends CommonController
{
    private $brand_role_id;
    private $admin_user;
    private $is_brand = 0;

    public function _initialize(){
        parent::_initialize();
        $this->brand_role_id = C('BRAND_ROLE_ID');
        $this->admin_user = session('LG_USER');
        if($this->admin_user['role_id'] == $this->brand_role_id ){
            $this->is_brand = 1;
        }
        $this->assign('brand_role_id', $this->brand_role_id);
        $this->assign('admin_user', $this->admin_user);
        $this->assign('is_brand', $this->is_brand);
    }

    public function index()
    {
        $model = M('brand');
		$Count = $model->count();
		$Page = new \Think\Pager($Count,10);
		$show = $Page->showhtml();
		$keyword = I('keyword');
        $where = ['check_status'=>1];
		if(!empty($keyword)){
			$where['brand_name|name|mobile'] = array('like',"%".$keyword."%");
		}

		$list = $model->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('is_top desc,sort asc')->select();
		if($list){
			foreach($list as $key=>&$row){
				$row['status'] = $row['status'] ? '开启' : '关闭';
			}
		}
		$this->assign('page',$show);
		$this->assign('list', $list);
		$this->display();
    }

    public function add()
    {
		if(IS_POST){
			$data = I('post.');
            if(empty($data['name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入姓名'));
            }
            if(empty($data['mobile'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入联系电话'));
            }
            if(empty($data['hotline'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入厂家热线'));
            }
            if(empty($data['logo'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入品牌标识'));
            }
            if(empty($data['imgs'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传轮播图片'));
            }
            if(empty($data['brand_name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入品牌名称'));
            }
            // if(empty($data['pcd'])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            // $pcd = explode("/", $data['pcd']);
            // if(empty($pcd[0])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择省份'));
            // }
            // if(empty($pcd[1])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择城市'));
            // }
            // if(empty($pcd[2])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            if(empty($data['address'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入详细地址'));
            }
            if(empty($data['product_name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入产品名称'));
            }
            // $data['logo'] = isset($data['logo'][0]) ? $data['logo'][0] : '';
            // $data['imgs'] = !empty($data['imgs']) ? implode(',', $data['imgs']) : '';
            $data['content'] = $data['content'] ? html_entity_decode($data['content']) : '';
            // $data['imgs'] = !empty($data['imgs']) ? implode(',', $data['imgs']) : '';
            // $data['province'] = $pcd[0];
            // $data['city'] = $pcd[1];
            // $data['district'] = $pcd[2];
            $data['check_status'] = 1;

            $model = M('brand');

            $user_model = M('user');
            $user = $user_model->where(['phone'=>$data['mobile']])->find();
            if($user){
                $user_id = $user['id'];
            }else{
                $user_id = user_reg([
                    'img'       => C('SiteUrl').'Public/upload/userimg/t.jpg',
                    'nickname'  => "用户".$data['mobile'],
                    'phone'     => $data['mobile'],
                    'pwd'       => md5(123456),
                    'ctime'     => time(),
                ]);
                if(!$user_id){
                    $this->ajaxReturn(array('s'=>false,'m'=>'用户添加失败!'));
                }
            }
            $data['uid'] = $user_id;
            $data['create_time'] = time();

			$id = $model->add($data);
			if($id){
				$this->ajaxReturn(array('s'=>true,'m'=>'添加成功', 'url'=>U('index')));
			}else{
				$this->ajaxReturn(array('s'=>false,'m'=>'添加失败!'));
			}
		}else{
            $admin_arr = M('rbac_manager')->alias('rm')->join('left join __RBAC_ROLE_MANAGER__ rrm on rm.id=rrm.user_id')->where(['rrm.role_id'=>$this->brand_role_id, 'rm.status'=>1])->getField('rm.id,rm.username', true);

            $advertising = M('Article_advertising')->select();
            $this->assign('advertising', $advertising);
            $this->assign('admin_arr', $admin_arr);
			$this->display();
		}
    }

    public function edit()
    {
		$id = I('id');
        if(IS_POST){
			$data = I('post.');
            if(empty($data['name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入姓名'));
            }
            if(empty($data['mobile'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入联系电话'));
            }
            if(empty($data['hotline'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入厂家热线'));
            }
            if(empty($data['logo'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入品牌标识'));
            }
            if(empty($data['imgs'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传轮播图片'));
            }
            if(empty($data['brand_name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入品牌名称'));
            }
            // if(empty($data['pcd'])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            // $pcd = explode("/", $data['pcd']);
            // if(empty($pcd[0])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择省份'));
            // }
            // if(empty($pcd[1])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择城市'));
            // }
            // if(empty($pcd[2])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            if(empty($data['address'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入详细地址'));
            }
            if(empty($data['product_name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入产品名称'));
            }
            // $data['logo'] = isset($data['logo'][0]) ? $data['logo'][0] : '';
            // $data['imgs'] = !empty($data['imgs']) ? implode(',', $data['imgs']) : '';
            $data['content'] = $data['content'] ? html_entity_decode($data['content']) : '';
            // $data['imgs'] = !empty($data['imgs']) ? implode(',', $data['imgs']) : '';
            // $data['province'] = $pcd[0];
            // $data['city'] = $pcd[1];
            // $data['district'] = $pcd[2];

            $model = M('brand');

			$res = $model->where(['id'=>$id])->save($data);
			if($res!==false){
				$this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('index')));
			}else{
				$this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
			}
		}else{
			$info = M('brand')->where(['id'=>$id])->find();
            $admin_arr = M('rbac_manager')->alias('rm')->join('left join __RBAC_ROLE_MANAGER__ rrm on rm.id=rrm.user_id')->where(['rrm.role_id'=>$this->brand_role_id, 'rm.status'=>1])->getField('rm.id,rm.username', true);

            $advertising = M('Article_advertising')->select();
            $this->assign('advertising', $advertising);
            $this->assign('admin_arr', $admin_arr);
			$this->assign('info', $info);
			$this->display();
		}
    }
    
    public function apply(){
        $model = M('brand');
		$Count = $model->count();
		$Page = new \Think\Pager($Count, 10);
		$show = $Page->showhtml();
		$keyword = I('keyword');
        $where = ['check_status'=>['in', [0]]];
		if(!empty($keyword)){
			$where['brand_name|name|mobile'] = array('like',"%".$keyword."%");
		}

		$list = $model->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
		if($list){
            $status_arr = ['申请中', '同意', '拒绝'];
			foreach($list as $key=>&$row){
				$row['status'] = $status_arr[$row['check_status']];
			}
		}
		$this->assign('page',$show);
		$this->assign('list', $list);
		$this->display();
    }

    public function apply_edit(){
        $id = I('id');
        if(IS_POST){
			$data = I('post.');
            if(empty($data['name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入姓名'));
            }
            if(empty($data['mobile'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入联系电话'));
            }
            if(empty($data['logo'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入品牌标识'));
            }
            if(empty($data['imgs'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传轮播图片'));
            }
            if(empty($data['brand_name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入品牌名称'));
            }
            // if(empty($data['pcd'])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            // $pcd = explode("/", $data['pcd']);
            // if(empty($pcd[0])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择省份'));
            // }
            // if(empty($pcd[1])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择城市'));
            // }
            // if(empty($pcd[2])){
            //     $this->ajaxReturn(array('s'=>false,'m'=>'请选择地区'));
            // }
            if(empty($data['address'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入详细地址'));
            }
            if(empty($data['product_name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入产品名称'));
            }
            // $data['logo'] = isset($data['logo'][0]) ? $data['logo'][0] : '';
            // $data['imgs'] = !empty($data['imgs']) ? implode(',', $data['imgs']) : '';
            $data['content'] = $data['content'] ? html_entity_decode($data['content']) : '';
            // $data['imgs'] = !empty($data['imgs']) ? implode(',', $data['imgs']) : '';
            // $data['province'] = $pcd[0];
            // $data['city'] = $pcd[1];
            // $data['district'] = $pcd[2];
            if($data['check_status']){
                $data['check_time'] = time();
                if($data['check_status'] == 1){
                    $data['status'] = 1;
                }
            }
            $model = M('brand');

			$res = $model->where(['id'=>$id])->save($data);
			if($res!==false){
				$this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('apply')));
			}else{
				$this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
			}
		}else{
			$info = M('brand')->where(['id'=>$id])->find();
            $admin_arr = M('rbac_manager')->alias('rm')->join('left join __RBAC_ROLE_MANAGER__ rrm on rm.id=rrm.user_id')->where(['rrm.role_id'=>$this->brand_role_id, 'rm.status'=>1])->getField('rm.id,rm.username', true);
            $this->assign('admin_arr', $admin_arr);
			$this->assign('info', $info);
			$this->display();
		}
    }

    public function del(){
        $id = I('id');
        $res = M('brand')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('index')));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }
    }

    public function top($id)
    {
        $info = M('brand')->where(['id'=>$id])->find();
        if(empty($info)){
            $this->error('操作失败');
        }
        $data = [
            'is_top'  => $info['is_top'] ? 0 : 1,
        ];
        $res = M('brand')->where(['id'=>$id])->save($data);
        if($res!==false){
            $this->success('操作成功', U('index'));
        }else{
            $this->error('操作失败');
        }        
    }

    public function product_edit()
    {
        $id = I('id');
        $info = M('brand')->where(['admin_id'=>$this->admin_user['id']])->find();
        $id = $info ? $info['id'] : '';
        if(IS_POST){
            $data = I('post.');
            if(empty($data['product_name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入产品名称'));
            }
            $data['content'] = $data['content'] ? html_entity_decode($data['content']) : '';
            $model = M('brand');
            if($info['product_name'] !=$data['product_name']  || $info['content'] != $data['content']){
                $res = M('brand_product')->add([
                        'brand_id'  => $id,
                        'product_name' => $data['product_name'],
                        'product_content' => $data['content'],
                        'status' => 0,
                        'create_time'   => time(),
                    ]);
            }else{
                $this->ajaxReturn(array('s'=>true,'m'=>'提交成功', 'url'=>U('product_edit')));
            }
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'提交成功', 'url'=>U('product_edit')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function product_apply()
    {
        $model = M('brand_product');
        $Count = $model->count();
        $Page = new \Think\Pager($Count,10);
        $show = $Page->showhtml();
        $keyword = I('keyword');
        $where = [];
        if(!empty($keyword)){
            $where['b.brand_name'] = array('like',"%".$keyword."%");
        }

        $list = $model->alias('bp')->join('left join __BRAND__ b on bp.brand_id=b.id')
                ->where($where)
                ->limit($Page->firstRow.','.$Page->listRows)
                ->order('bp.create_time desc')
                ->field('bp.*,b.brand_name')
                ->select();
        if($list){
            $status_arr = ['0'=>'申请中', '1'=>'已同意', '2'=>'已拒绝'];
            foreach($list as $key=>&$row){
                $row['status'] = $status_arr[$row['status']];
            }
        }
        $this->assign('page',$show);
        $this->assign('list', $list);
        $this->display();        
    }

    public function product_info()
    {
        $id = I('id');
        $info = M('brand_product')->where(['id'=>$id])->find();
        if(IS_POST){
            $data = I('post.');
            $status = I('status');
            $data['product_content'] = $data['product_content'] ? html_entity_decode($data['product_content']) : '';
            $res = M('brand_product')->where(['id'=>$id])->save($data);
            if($status == 1){
                $res2 = M('brand')->where(['id'=>$info['brand_id']])->save([
                        'product_name'=>$data['product_name'],
                        'content'=>$data['product_content']
                    ]);
            }
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('product_apply')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $this->assign('info', $info);
            $this->display();
        }        
    }

    public function product_del($value='')
    {
        $id = I('id');
        $res = M('brand_product')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('product_apply')));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }
    }
}
